package pri.abner.practice.thread.sequentialexecution;
/**
 *@description 使用join保证顺序执行
 *@author abner chow
 *@date 2019-08-30 11:07
 */
public class Execution1 {

    public static void main(String[] args) {

        Thread t1 = new Thread(new Work("Thread one", null));
        Thread t2 = new Thread(new Work("Thread two", t1));
        Thread t3 = new Thread(new Work("Thread three", t2));

        t1.start();
        t2.start();
        t3.start();
    }


    static class Work implements Runnable {

        /**
         * 前一个线程
         */
        private Thread pre;

        /**
         * 线程名
         */
        private String name;

        Work(String name, Thread pre) {
            this.name = name;
            this.pre = pre;
        }

        @Override
        public void run() {
            if (null != pre) {
                try {
                    pre.join();
                    System.out.println(this.name + " start");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(this.name + " start");
            }
        }
    }


}
